package io.plus.client;

import io.plus.client.user_interface.ClientAioHandler;
import io.plus.client.user_interface.ClientAioListener;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.interactive.Configuration;
import io.plus.interactive.user_interface.AioHandler;
import io.plus.interactive.user_interface.AioListener;
import io.plus.utils.lock.ReadWriteLockSet;
import io.plus.utils.thread.lock_capable_task.LockCapableRunnableThreadPoolExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Data
public class ClientConfiguration extends Configuration {
    //static Logger log = LoggerFactory.getLogger(ClientConfiguration.class);
    public final ReadWriteLockSet<AsynchronousSocketChannelContext> connecteds = new ReadWriteLockSet<AsynchronousSocketChannelContext>(new HashSet<AsynchronousSocketChannelContext>());
    public final ReadWriteLockSet<AsynchronousSocketChannelContext> closeds = new ReadWriteLockSet<AsynchronousSocketChannelContext>(new HashSet<AsynchronousSocketChannelContext>());
    protected ReConnectConfiguration reConnectConfiguration;//重连配置
    private ClientAioHandler clientAioHandler = null;
    private ClientAioListener clientAioListener = null;
    private ConnectionCompletionHandler connectionCompletionHandler = new ConnectionCompletionHandler();

    /**
     * 不重连
     */
    public ClientConfiguration(ClientAioHandler aioHandler, ClientAioListener aioListener) {
        this(aioHandler, aioListener, null);
    }

    /**
     * @param aioHandler
     * @param aioListener
     * @param reConnectConfiguration 不用框架自动重连，就传null
     */
    public ClientConfiguration(ClientAioHandler aioHandler, ClientAioListener aioListener, ReConnectConfiguration reConnectConfiguration) {
        this(aioHandler, aioListener, reConnectConfiguration, null, null);
    }

    /**
     * @param clientAioHandler
     * @param clientAioListener
     * @param reConnectConfiguration 不用框架自动重连，就传null
     * @param tioExecutor
     * @param groupExecutor
     */
    public ClientConfiguration(ClientAioHandler clientAioHandler, ClientAioListener clientAioListener, ReConnectConfiguration reConnectConfiguration, LockCapableRunnableThreadPoolExecutor tioExecutor,
                               ThreadPoolExecutor groupExecutor) {
        super(tioExecutor, groupExecutor);

        this.setClientAioHandler(clientAioHandler);
        this.setClientAioListener(clientAioListener);

        this.reConnectConfiguration = reConnectConfiguration;
    }

    @Override
    public AioHandler getAioHandler() {
        return this.getClientAioHandler();
    }

    @Override
    public AioListener getAioListener() {
        return this.getClientAioListener();
    }


    public void setClientAioListener(ClientAioListener clientAioListener) {
        this.clientAioListener = clientAioListener;
        if (this.clientAioListener == null) {
            this.clientAioListener = new DefaultClientAioListener();
        }
    }


    @Override
    public boolean isServer() {
        return false;
    }

    @Override
    public String toString() {
        return "ClientConfiguration [name=" + name + "]";
    }
}
